#!/bin/bash
# 打包(根目录)：tar -zcvf src.tar.gz src
# sudo wget -O /tmp/src.tar.gz https://gitee.com/gogls/docker-tool/raw/master/src.tar.gz&&tar -C /tmp -zxvf /tmp/src.tar.gz&&cd /tmp/src/&&sudo bash /tmp/src/start.sh

if [ $(whoami) != "root" ];then
	echo -e "\033[31m 请使用root权限执行安装命令！"
	exit 1;
fi

# 必须是64位系统
is64bit=$(getconf LONG_BIT)
if [ "${is64bit}" != '64' ];then
	echo -e "\033[31m 抱歉, 不支持32位系统, 请使用64位系统";
	exit 1;
fi

# 如果有值，便是ubuntu系统。返回ubuntu系统版本号，
UbuntuCheck=$(cat /etc/issue|grep Ubuntu|awk '{print $2}'|cut -f 1 -d '.')
if [ "${UbuntuCheck}" ] && [ "${UbuntuCheck}" -lt "16" ];then
	echo -e "\033[31m Ubuntu ${UbuntuCheck}不支持，建议使用Ubuntu22"
	exit 1;
fi

# 获取用户所处的环境：prod、dev
read -p "请问你将部署到什么环境？[dev/prod] " input_env_value


echo "这里是 docker 自动化部署套件。输入 exit 退出。"

echo "----------------------环境管理-------------------------------"
echo "  安装docker：install docker"
echo "  卸载docker：remove docker"
echo "  安装docker-compose：install compose"
echo "  卸载docker-compose：remove compose"
echo "  查看容器列表：docker ps"
echo "  查看镜像列表：docker images"
echo "  docker常用命令：docker help"
echo "  安装portainer：install portainer"
echo "  卸载portainer：remove portainer"


echo "----------------------部署项目-------------------------------"
# 如果是开发环境
if [ "$input_env_value" == "dev" ];then
  echo "  安装mineadmin：install mineadmin"
  echo "  安装fastadmin：install fastadmin"
  echo "  安装unichat：install unichat"
  echo "  地方政府服务平台：install government platform"
# 如果是生产环境
elif [ "$input_env_value" == "prod" ];then
  echo "  安装unichat：install unichat"
else
  echo -e "\033[31m 请输入一个正确的环境值"
  exit 1;
fi


# 地方政府服务平台
function install_government-platform() {
  if [ "$input_env_value" == "dev" ];then

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./develop/docker-compose.yml up -d government-platform

    # 休息一秒钟
    sleep 1
    echo "部署前端..."
    # 部署前端
    docker exec -it dev_government-platform bash -c "git clone https://gitee.com/wang-haifeng2021/dandongdifangqianduan.git /www/government-platform-vue && cd /www/government-platform-vue && apk add yarn && yarn install"
    echo "部署后端..."
    # 部署后端
    docker exec -it dev_government-platform bash -c "git clone https://gitee.com/wang-haifeng2021/dandongdifanghoutai-toll-collection-system.git /www/government-platform && cd /www/government-platform && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install"

    # 重启容器
    docker restart dev_government-platform
    # 设置读写权限
    chmod -R 777 /opt/wwwroot/www/government-platform
    chmod -R 777 /opt/wwwroot/www/government-platform-vue

    current_ip=$(hostname -I | cut -d ' ' -f1)
    echo '----------------部署成功----------------'
    echo "后端：$current_ip:9301"
    echo "前端：$current_ip:2388"
  else
    echo -e "\033[31m $input_env_value 环境，还不支持此项目 \e[0m"
  fi
}

# 安装 mineadmin 项目
function install_mineadmin() {
  if [ "$input_env_value" == "dev" ];then

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./develop/docker-compose.yml up -d mineadmin

    # 休息一秒钟
    sleep 1
    echo "部署前端..."
    # 部署前端
    docker exec -it dev_mineadmin bash -c "git clone https://gitee.com/mineadmin/mineadmin-vue.git /www/mineadmin-vue && cd /www/mineadmin-vue && apk add yarn && yarn install"
    echo "部署后端..."
    # 部署后端
    docker exec -it dev_mineadmin bash -c "git clone https://gitee.com/mineadmin/mineadmin.git /www/mineadmin && cd /www/mineadmin && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install"

    # 重启容器
    docker restart dev_mineadmin
    # 设置读写权限
    chmod -R 777 /opt/wwwroot/www/mineadmin
    chmod -R 777 /opt/wwwroot/www/mineadmin-vue

    current_ip=$(hostname -I | cut -d ' ' -f1)
    echo '----------------部署成功----------------'
    echo "后端：$current_ip:9101"
    echo "前端：$current_ip:2188"
  else
    echo -e "\033[31m $input_env_value 环境，还不支持此项目 \e[0m"
  fi
}

# 安装 unichat 项目
function install_unichat() {
  if [ "$input_env_value" == "prod" ];then

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./production/docker-compose.yml up -d unichat

    # 休息一秒钟
    sleep 1
    echo "部署项目..."
    # 部署后端
    docker exec -it prod_unichat bash -c "git clone https://gitee.com/gogls/unichat.git /www/unichat && cd /www/unichat && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install"

    # 配置nginx
    cp production/ai.goooo.love.conf /opt/wwwroot/nginx/conf.d/

    # 重启容器
    docker restart prod_unichat
    docker restart prod_nginx

    current_ip=$(hostname -I | cut -d ' ' -f1)
    echo '----------------部署成功----------------'
    echo "后端：$current_ip:9101"
    echo "前端：src/production/ai.goooo.love.conf 里配置的域名，文件名可任意更改。"
  else

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./develop/docker-compose.yml up -d unichat

    # 休息一秒钟
    sleep 1
    echo "部署admin..."
    # 部署前端
    docker exec -it dev_unichat bash -c "git clone https://gitee.com/gogls/unichat-admin.git /www/unichat-admin && cd /www/unichat-admin && apk add yarn && yarn install"
    echo "部署主程序..."
    # 部署后端
    docker exec -it dev_unichat bash -c "git clone https://gitee.com/gogls/unichat.git /www/unichat && cd /www/unichat && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install"

    # 重启容器
    docker restart dev_unichat
    # 设置读写权限
    chmod -R 777 /opt/wwwroot/www/unichat
    chmod -R 777 /opt/wwwroot/www/unichat-admin

    current_ip=$(hostname -I | cut -d ' ' -f1)
    echo '----------------部署成功----------------'
    echo "后端：$current_ip:9201"
    echo "前端：$current_ip:2288"
  fi
}

# 安装docker
function install_docker() {
  # 更新系统
  apt-get update
  # 安装依赖
  apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
  # 添加 Docker 官方的 GPG 密钥
  if [ "$UbuntuCheck" ];then
    # ubuntu系统
    curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -
    # 设置稳定版本的apt仓库地址
    add-apt-repository \
         "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
         $(lsb_release -cs) \
         stable"
    # 安装指定版本的docker,版本是20.10.23
    apt-get install docker-ce=5:20.10.23~3-0~ubuntu-jammy docker-ce-cli=5:20.10.23~3-0~ubuntu-jammy containerd.io
  else
    # 否则，只支持debian
    curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | apt-key add -
    add-apt-repository \
         "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/debian \
         $(lsb_release -cs) \
         stable"

    apt-get install docker-ce=5:20.10.23~3-0~debian-buster docker-ce-cli=5:20.10.23~3-0~debian-buster containerd.io
  fi

  # 刷新源
  apt-get update
  sleep 1
  systemctl restart docker
}

# 卸载 docker
function remove_docker() {
  apt-get autoremove docker docker-ce docker-engine  docker.io  containerd runc -y
  dpkg -l | grep docker
  dpkg -l |grep ^rc|awk '{print $2}' |xargs dpkg -P # 删除无用的相关的配置文件
  apt-get autoremove docker-ce-* -y
  apt-get remove docker docker-engine docker.io containerd runc
  apt-get purge docker-ce
  ps -ef|grep docker|grep -v grep|xargs kill -s 9
  apt-get remove docker-*
  apt-get remove docker-ce-cli-*
  apt-get remove docker-scan-plugin*
  rm -rf /etc/systemd/system/docker.service.d
  rm -rf /var/lib/docker
  rm -rf /var/lib/containerd
  rm -rf /etc/docker
  rm -rf /run/docker
  rm -rf /var/lib/dockershim
}

# 安装docker-compose
function install_compose() {
  curl -L https://get.daocloud.io/docker/compose/releases/download/v2.15.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
  chmod +x /usr/local/bin/docker-compose
  ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  docker-compose version
}

# 入口函数
function go(){
  if [ "$install_type" ];then
    # 安装docker
    if [ "$install_type" == "install docker" ];then
      install_docker

		# 将docker卸载干净
    elif [ "$install_type" == "remove docker" ];then
      remove_docker

    # 安装docker-compose
    elif [ "$install_type" == "install compose" ];then
      install_compose

    # 卸载docker-compose
    elif [ "$install_type" == "remove compose" ];then
      rm -rf /usr/local/bin/docker-compose
      rm -rf /usr/bin/docker-compose

    # 查看容器列表
    elif [ "$install_type" == "docker ps" ];then
      docker ps

    # 查看镜像列表
    elif [ "$install_type" == "docker images" ]
    then
      docker images

    # 安装容器面板
    elif [ "$install_type" == "install portainer" ]
    then
      docker run -d -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer-ce

    # 卸载容器面板
    elif [ "$install_type" == "remove portainer" ];then
      docker rm -f portainer

    # 安装mineadmin项目
    elif [ "$install_type" == "install mineadmin" ];then
      install_mineadmin

    # 安装 unichat 项目
    elif [ "$install_type" == "install unichat" ];then
      install_unichat

   # 安装 地方政府服务平台 项目
    elif [ "$install_type" == "install government platform" ];then
      install_government-platform


   # docker
    elif [ "$install_type" == "docker help" ];then
      echo "  查看容器列表：docker ps -a"
      echo "  重启容器：docker restart 容器名"
      echo "  停止容器：docker stop 容器名"
      echo "  进入容器：docker exec -it 容器名 bash"
      echo "  删除容器：docker rm -f 容器名"
      echo "  查看镜像列表：docker images"
      echo "  删除镜像：docker rmi 镜像名"

    else
			echo -e "\033[31m 暂不支持你输入的命令 \e[0m"
    fi

  fi
}

# 死循环，这样可以多次处理
while [ true ]
do
	read -p "初始状态，请输入一个值：" install_type
	if [ "$install_type" ] && [ "$install_type" == "exit" ]
	then
		break
	fi
	go $install_type
done
